﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Tools.Linq;
using MobiMenu.Restaurant.Web.Utility;
using System.Collections.Specialized;
using System.Linq.Expressions;

namespace MobiMenu.Restaurant.Web
{
    /// <summary>
    ///Class1 的摘要说明
    /// </summary>
    public class OrderedDishesService : BaseRESTService<OrderedDish>
    {
        public OrderedDishesService()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        protected override Expression<Func<OrderedDish, bool>> GetWhereExpression(NameValueCollection nameValues)
        {
            var queryBuilder = QueryBuilder.Create<OrderedDish>();
            
            int[] ids;
            if (RequestHelper.GetInt32Array(nameValues, "Id", out ids))
            {
                queryBuilder = queryBuilder.In(c => c.Id, ids);
            }

            if (RequestHelper.GetInt32Array(nameValues, "DishId", out ids))
            {
                queryBuilder = queryBuilder.In(c => c.DishId, ids);
            }

            if (RequestHelper.GetInt32Array(nameValues, "OrderId", out ids))
            {
                queryBuilder = queryBuilder.In(c => c.OrderId, ids);
            }

            DateTime startTime;
            DateTime endTime;
            if (RequestHelper.GetDateTimeRange(nameValues, "OrderedTime", out startTime, out endTime))
            {
                queryBuilder = queryBuilder.Between(c => c.OrderedTime, startTime, endTime);            
            }

            OrderedDishStatus[] statuses;
            if (RequestHelper.GetEnumArray<OrderedDishStatus>(nameValues, "Status", out statuses))
            {
                queryBuilder = queryBuilder.In(c => c.Status, statuses);
            }

            return queryBuilder.Expression;
        }

        protected override bool ProcessOrder(string sortField, bool isDesc, ref IQueryable<OrderedDish> query)
        {
            if (string.IsNullOrEmpty(sortField))
            {
                return false;
            }

            if (sortField.Equals("Id", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<OrderedDish, int>> expr = c => c.Id;
                query = CommonHelper.QueryOrder<OrderedDish, int>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("OrderedTime", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<OrderedDish, DateTime>> expr = c => c.OrderedTime;
                query = CommonHelper.QueryOrder<OrderedDish, DateTime>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("Status", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<OrderedDish, OrderedDishStatus>> expr = c => c.Status;
                query = CommonHelper.QueryOrder<OrderedDish, OrderedDishStatus>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("DishId", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<OrderedDish, int>> expr = c => c.DishId;
                query = CommonHelper.QueryOrder<OrderedDish, int>(query, expr, isDesc);
                return true;
            }

            return false;
        }

        protected override bool GetDeleteWhereExpression(RESTContext<OrderedDish> context,
            out Expression<Func<OrderedDish, bool>> expr)
        {
            expr = null;
            if (context == null || context.RequestData == null
                || context.RequestData.DeleteIds == null || context.RequestData.DeleteIds.Count == 0)
            {
                return false;
            }

            var queryBuilder = QueryBuilder.Create<OrderedDish>();
            queryBuilder = queryBuilder.In(c => c.Id, context.RequestData.DeleteIds.ToArray());
            expr = queryBuilder.Expression;
            return true;
        }

        protected override bool UpdateEntity(NameValueCollection nameValues, OrderedDish item)
        {
            bool changed = false;
            int dishId;
            if (RequestHelper.GetInt32(nameValues, "DishId", out dishId))
            {
                item.DishId = dishId;
                changed = true;
            }

            int orderId;
            if (RequestHelper.GetInt32(nameValues, "OrderId", out orderId))
            {
                item.OrderId = orderId;
                changed = true;
            }

            DateTime dt;
            if (RequestHelper.GetDateTime(nameValues, "OrderedTime", out dt))
            {
                item.OrderedTime = dt;
                changed = true;
            }

            OrderedDishStatus status;
            if (RequestHelper.GetEnum<OrderedDishStatus>(nameValues, "Status", out status))
            {
                item.Status = status;
                changed = true;
            }

            return changed;
        }

        protected override bool GetUpdateWhereExpression(NameValueCollection nameValues, out Expression<Func<OrderedDish, bool>> expr)
        {
            var queryBuilder = QueryBuilder.Create<OrderedDish>();
            int id;
            if (RequestHelper.GetInt32(nameValues, "Id", out id))
            {
                queryBuilder = queryBuilder.Equals(c => c.Id, id);
                expr = queryBuilder.Expression;
                return true;
            }

            expr = null;
            return false;
        }
    }
}